var returnurl=encodeURIComponent("http://www.pulu.com/weixin/pulu.html"); //同意后跳转页面
$(document).ready(function(){
    if(is_weixn()){
        if($.cookie("MYID")){ //$.cookie("wxopenid")
            //===============================================================================
            //从登录页返回有openid没有id
            if(!$.cookie("MYID")){location.href="http://www.pulu.com/weixin/login.html"}
            //===============================================================================
        }
        else{
            //先微信验证获取openid 如果已经授权过了 直接页面截取openid 然后发送请求获取 用户的ID
            var reg=new RegExp("code=[a-zA-Z0-9]*");
            if(window.location.href.match(reg)){
                var code=window.location.href.match(reg);
                $.ajax({
                    type:"GET",
                    url:"GetOpenId.ashx?"+code,
                    async: false,
                    success:function(data){
                        console.log(data);
                        var dat=data;
                        $.cookie("wxod",dat);
                        $.cookie("wxopenid",dat);
                        //发请求获取用户的id
                        $.ajax({
                            type:"GET",
                            url:getwxopenidurl+dat,
                            success:function(d){
                                //如果没有用户id 则证明用户是第一次登陆 保存openid跳转至登录页面
                                if(!d.Data||!d.Data.userId){
                                    //微信跳转进来的
                                    if(!$.cookie("MYID")||$.cookie("MYID").length<10){
                                        $.cookie("wxopenid",dat);
                                        location.href="http://www.pulu.com/weixin/login.html"
                                    }
                                    //退出登录的
                                }else if(d.Data&&d.Data.userId.length>0){
                                    $.cookie("wxopenid",d.Data.openId);
                                    $.cookie("MYID",d.Data.userId);
                                    $.cookie("MYAVA",d.Data.userAva);
                                    $.cookie("MYNICK",d.Data.userNick);
                                    //id=d.Data.userId;
                                    location.href="http://www.pulu.com/weixin/pulu.html"
                                }
                            }
                        })
                    }
                })
            }
            else{
                location.href="https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx658c34793ecfd9f9&redirect_uri="+returnurl+"&response_type=code&scope=snsapi_base&state=STATE#wechat_redirect";
            }
        }
    }
    else{
        if(!$.cookie("MYID")){location.href="http://www.pulu.com/weixin/login.html"};
    }
});
//if(!$.cookie("MYID")){location.href="https://www.pulu.com/weixin/login.html"};
//if(!/https/.test(location.href)){location.href="https://www.pulu.com/weixin/pulu.html"};
/*
 2滚动
 */
var mySwiper = new Swiper ('#swiper-container', {
    autoplay : 2500,
    speed:300,
    pagination : '.swiper-pagination',
    loop : true
});
var mySwiper2 = new Swiper ('#swiper-container2', {
    autoplay : 2000,
    speed:300,
    loop : true
});
var secondTim=setTimeout(function(){
    var mySwiper3 = new Swiper ('#swiper-container3', {
        autoplay : 2000,
        speed:300,
        loop : true
    });
    clearTimeout(secondTim);
},1000);

/*
 3翻转
 */
//var eleBack = null, eleFront = null,
//    eleList=$("#box .list");
//// 确定前面与后面元素
//var turnAround = function() {
//    eleList.each(function() {
//        if ($(this).hasClass("out")) {
//            eleBack = $(this);
//        } else {
//            eleFront = $(this);
//        }
//    });
//};
//function returnback(){
//    eleList = $("#box .list");
//    eleFront.addClass("out").removeClass("in");
//    setTimeout(function() {
//        eleBack.addClass("in").removeClass("out");
//        // 重新确定正反元素
//        turnAround();
//    }, 225);
//    return false;
//}
////初始前后元素
//turnAround();
////定时器开始效果
//var timer=setTimeout("showback()",2000);
//function showback(){
//    eleList = $("#box .list");
//    eleFront.addClass("out").removeClass("in");
//    setTimeout(function() {
//        eleBack.addClass("in").removeClass("out");
//        // 重新确定正反元素
//        turnAround();
//        setTimeout("returnback()",5000);
//
//    }, 225);
//    return false;
//}

/*
 跳转
 */
//问题
$("#sendquebtn").click(function(){
    //if(is_weixn()){
    $.cookie("expkeyword","");
    $.cookie("expType","");
    $.cookie("sendque","question");
    //location.href="https://www.pulu.com/weixin/sendquestion.html"
    location.href="http://www.pulu.com/weixin/sendexpgpc.html";
    //}else{
    //    //location.href="https://www.pulu.com/sendquestion.html?id="+$.cookie("MYID");
    //    location.href="https://www.pulu.com/sendexpgpc.html?id="+$.cookie("MYID");
    //}
});
//经验
$("#box").click(function(){
    //$("#quickexplist").css("display","block")
    if(is_weixn()){
        $.cookie("expkeyword","");
        $.cookie("expType","");
        $.cookie("sendque","");
        location.href="http://www.pulu.com/weixin/sendexpgpc.html";
    }else{
        location.href="https://www.pulu.com/weixin/sendexpgpc.html?id="+$.cookie("MYID");
    }
});
$("#nomexp").click(function(){
    if(is_weixn()){
        $.cookie("expkeyword","");
        $.cookie("expType","");
        $.cookie("sendque","");
        location.href="http://www.pulu.com/weixin/sendexpgpc.html";
    }else{
        location.href="https://www.pulu.com/weixin/sendexpgpc.html?id="+$.cookie("MYID");
    }
});
$("#heiexp").click(function(){
    if(is_weixn()){
        $.cookie("expkeyword","");
        $.cookie("expType","");
        $.cookie("sendque","");
        location.href="http://www.pulu.com/weixin/sendexpgpc.html";
    }else{
        location.href="http://www.pulu.com/sendexpgpc.html?id="+$.cookie("MYID");
    }
});
//新闻
$("#ydq").click(function(){
    if(ispcword=="yes"){location.href="http://www.pulu.com/weixin/homepc.html"}
    else{location.href="http://www.pulu.com/weixin/home.html";$.cookie("newstype",0)}
});
$("#zhnews").click(function(){
    if(ispcword=="yes"){location.href="http://www.pulu.com/weixin/zonghenews.html";$.cookie("newstype",1)}
    else{location.href="http://www.pulu.com/weixin/home.html";$.cookie("newstype",1)}
});
$("#hynews").click(function(){
    if(ispcword=="yes"){location.href="http://www.pulu.com/weixin/zonghenews.html";$.cookie("newstype",2)}
    else{location.href="http://www.pulu.com/weixin/home.html";$.cookie("newstype",2)}
});
$("#xqnews").click(function(){
    if(ispcword=="yes"){location.href="http://www.pulu.com/weixin/zonghenews.html";$.cookie("newstype",3)}
    else{location.href="http://www.pulu.com/weixin/home.html";$.cookie("newstype",3)}
});

/*
 * banner
 */
var en;
$.ajax({
    type:"get",
    dataType: "json",
    url:"https://www.pulu.com/webapi/api/puluquestion/GetPuluTopList",
    success:function(data){
        if(ispcword=="yes"){
            $("#nav1 img").attr("src","http://42.62.66.198:8006"+data.Data[0].imgpath1);
            $("#nav2 img").attr("src","http://42.62.66.198:8006"+data.Data[1].imgpath1);
            $("#nav3 img").attr("src","http://42.62.66.198:8006"+data.Data[2].imgpath1);
            $("#nav4 img").attr("src","http://42.62.66.198:8006"+data.Data[3].imgpath1);
            $("#nav5 img").attr("src","http://42.62.66.198:8006"+data.Data[4].imgpath1);
            $("#nav6 img").attr("src","http://42.62.66.198:8006"+data.Data[5].imgpath1);
        }
        else{
            $("#nav1 img").attr("src","http://42.62.66.198:8006"+data.Data[0].imgpath);
            $("#nav2 img").attr("src","http://42.62.66.198:8006"+data.Data[1].imgpath);
            $("#nav3 img").attr("src","http://42.62.66.198:8006"+data.Data[2].imgpath);
            $("#nav4 img").attr("src","http://42.62.66.198:8006"+data.Data[3].imgpath);
            $("#nav5 img").attr("src","http://42.62.66.198:8006"+data.Data[4].imgpath);
            $("#nav6 img").attr("src","http://42.62.66.198:8006"+data.Data[5].imgpath);
        }
    }
});
//$(".swiper-slide").click(function(e){
//    var num=e.target.dataset.num;
//    if(en){
//        if(en[num].contenttype==1){
//            location.href="http://www.pulu.com/weixin/newsdetail.html?newsid="+en[num].newsid+"&fromuser="+id+"&newsShareId="+en[num].contentid;
//        }
//        else if(en[num].contenttype==2){
//            location.href="https://www.pulu.com/weixin/questiondetail.html?qid="+en[num].contentid+"#fromuser="+id;
//        }else if(en[num].contenttype==3){
//            location.href="https://www.pulu.com/weixin/shareexppage.html?eid="+en[num].contentid+"#fromuser="+id;
//        }
//    }
//});

/*
 * 每个类别获取三条
 */
if($.cookie("MYID")){
    //时间 几天前 几分钟前
    function getDateTimeStamp(dateStr){
        dateStr=dateStr.match(/[0-9\-T\:]+/)[0].replace(/T/g,' ');
        return Date.parse(dateStr.replace(/-/gi,"/"));
    }
    function getDateDiff(dateTimeStamp){
        var minute = 1000 * 60;
        var hour = minute * 60;
        var day = hour * 24;
        var halfamonth = day * 15;
        var month = day * 30;
        var now = new Date().getTime();
        var diffValue = now - dateTimeStamp;
        if(diffValue < 0){ result="刚刚";return;}
        var monthC =diffValue/month;
        var weekC =diffValue/(7*day);
        var dayC =diffValue/day;
        var hourC =diffValue/hour;
        var minC =diffValue/minute;
        if(monthC>=1){
            result="" + parseInt(monthC) + "月前";
        }
        else if(weekC>=1){
            result="" + parseInt(weekC) + "周前";
        }
        else if(dayC>=1){
            result=""+ parseInt(dayC) +"天前";
        }
        else if(hourC>=1){
            result=""+ parseInt(hourC) +"小时前";
        }
        else if(minC>=1){
            result=""+ parseInt(minC) +"分钟前";
        }else
            result="刚刚";
        if(result&&result!="undefined"){
            return result;
        }
        else{
            result="刚刚";
            return result;
        }
    }
    //经验
    $.ajax({
        type:"get",
        datatype:"json",
        url:expurl+"?userId="+id+"&currentIndex=1&pageSize=10",
        success:function(d){
            if(d.Data){
                $.each(d.Data,function(){
                    var tit=codetoface(this.title);
                    var tim="";
                    var userava="../img/af8.png";
                    var usernick=this.questionUserNick;
                    if(this.ctime){
                        tim=getDateDiff(getDateTimeStamp(this.ctime));
                        if(!tim){tim="刚刚"}
                    }
                    if(this.shareTime){
                        tim=getDateDiff(getDateTimeStamp(this.shareTime));
                        if(!tim){tim="刚刚"}
                    }
                    if(this.questionUserAva){userava=imgurl+this.questionUserAva}
                    if(this.ishidename){
                        userava="../img/af8.png";
                        usernick="匿名";
                    }
                    $(".threeExp").append("<li class='list'><div class='lf userlistava'><div><img src='"+userava+"'></div></div><span class='rt'>"+tim+"</span><a href='http://www.pulu.com/weixin/shareexppage.html?eid="+this.id+"#fromuser="+ $.cookie("MYID")+"'><span class='userlistnick'>"+usernick+"</span><span class='tit'>"+tit+"</span></a></li>");
                })
            }
        }
    });
    //问题
    $.ajax({
        type:"get",
        datatype:"json",
        url:waitanswerurl+"?userId="+id+"&currentIndex=1&pageSize=10",
        success:function(d){
            if(d.Data){
                $.each(d.Data,function(){
                    var tit=codetoface(this.title);
                    var tim="";
                    var userava="../img/af8.png";
                    var usernick=this.questionUserNick;
                    if(this.ctime){
                        tim=getDateDiff(getDateTimeStamp(this.ctime));
                        if(!tim){tim="刚刚"}
                    }
                    if(this.shareTime){
                        tim=getDateDiff(getDateTimeStamp(this.shareTime));
                        if(!tim){tim="刚刚"}
                    }
                    if(this.questionUserAva){userava=imgurl+this.questionUserAva}
                    if(this.ishidename){
                        userava="../img/af8.png";
                        usernick="匿名";
                    }
                    $(".threeQue").append("<li class='list'><div class='lf userlistava'><div><img src='"+userava+"'></div></div><span class='rt'>"+tim+"</span><a href='http://www.pulu.com/weixin/questiondetail.html?qid="+this.id+"#fromuser="+ $.cookie("MYID")+"'><span class='userlistnick'>"+usernick+"</span><span class='tit'>"+tit+"</span></a></li>");
                })
            }
        }
    });
    //同路人
    $.ajax({
        type:"get",
        datatype:"json",
        url:"https://www.pulu.com/webapi/api/puluuser/GetCommon3Users?userid="+id+"&currentIndex=1&pageSize=3",
        success:function(d){
            if(d.Data){
                $.each(d.Data,function(){
                    var userimg="";
                    var sex="man";
                    if(this.headimg){
                        userimg=imgurl+this.headimg;
                    }else{
                        userimg="../img/af8.png";
                    }
                    if(this.sex==0){sex="woman"}
                    $(".threeFel").append("<div class='list'> <a href='http://www.pulu.com/weixin/otherpeoplepage.html?otherid="+this.userId+"'> <div class='ava'><img src='"+userimg+"'/></div> <div class='nick'>"+this.nick+"<i class='"+sex+"'></i></div> </a> </div>");
                })
            }
        }
    });
    //新闻
    $.ajax({
        type:"get",
        datatype:"json",
        url:"https://www.pulu.com/webapi/api/puluquestion/GetShareNews3UserNews?userid="+id+"&currentIndex=1&pageSize=10",
        success:function(d){
            if(d.Data){
                $.each(d.Data,function(){
                    var tit=codetoface(this.title);
                    var tim="";
                    var userava="../img/af8.png";
                    var usernick=this.nick;
                    if(this.shareTime){
                        tim=getDateDiff(getDateTimeStamp(this.shareTime));
                        if(!tim){tim="刚刚"}
                    }
                    if(this.isStranger===0){//系统
                        userava="img/puluweixin.jpg";
                        usernick=this.sitename;
                    }else{//转发
                        if(this.userAva){userava=imgurl+this.userAva};
                    }
                    $(".threeNews").append("<li class='list'><div class='lf userlistava'><div><img src='"+userava+"'></div></div><span class='rt'>"+tim+"</span><a href='http://www.pulu.com/weixin/newsdetail.html?newsid="+this.id+"#fromuser="+ $.cookie("MYID")+"#newsShareId="+this.shareId+"#homepage=1'><span class='userlistnick'>"+usernick+"</span><span class='tit'>"+tit+"</span></a></li>");
                })
            }
        }
    });
}

/*
 4排行榜
 */
//收入
var inimgid=[];
$.ajax({
    type:"get",
    url:"https://www.pulu.com/webapi/api/puluquestion/GetInTopScore?userId="+id,
    dataType: "json",
    success:function(d){
        //console.log(d);
        //inimgid=[d.Data[0].userId,d.Data[1].userId,d.Data[2].userId,d.Data[3].userId,d.Data[4].userId];
        $(d.Data).each(function(i,e){
            inimgid.push(d.Data[i].userId);
            var n=i+1;
            if($(this)[0].headImg){
                $("#incomelist .effect-item:nth-child("+n+") img").attr("src","http://42.62.66.198:8003/"+$(this)[0].headImg);
            }else{
                $("#incomelist .effect-item:nth-child("+n+") img").attr("src","img/af8.png");
            }
            $("#incomelist .effect-item:nth-child("+n+") img").data("num",i);
            $("#incomelist .effect-item:nth-child("+n+") h3 a").html($(this)[0].userNick);
            $("#incomelist .effect-item:nth-child("+n+") h3 a").attr("href","http://www.pulu.com/weixin/otherpeoplepage.html?otherid="+$(this)[0].userId);
            $("#incomelist .effect-item:nth-child("+n+") .allday").html($(this)[0].daysEarn/100);
            $("#incomelist .effect-item:nth-child("+n+") .oneday").html($(this)[0].dayEarn/100);
        })
    }
});
$("#incomelist").delegate("img","click",function(e){
    var num=$(e.target).data("num");
    location.href="http://www.pulu.com/weixin/otherpeoplepage.html?otherid="+inimgid[num];
});
//支出
var payimgid=[];
$.ajax({
    type:"get",
    url:"https://www.pulu.com/webapi/api/puluquestion/GetOutTopScore?userId="+id,
    dataType: "json",
    success:function(d){
        //console.log(d);
        //payimgid=[d.Data[0].userId,d.Data[1].userId,d.Data[2].userId,d.Data[3].userId,d.Data[4].userId];
        $(d.Data).each(function(i,e){
            payimgid.push(d.Data[i].userId);
            var n=i+1;
            if($(this)[0].headImg){
                $("#paylist .effect-item:nth-child("+n+") img").attr("src","http://42.62.66.198:8003/"+$(this)[0].headImg);
            }else{
                $("#paylist .effect-item:nth-child("+n+") img").attr("src","img/af8.png");
            }
            $("#paylist .effect-item:nth-child("+n+") img").data("num",i);
            $("#paylist .effect-item:nth-child("+n+") h3 a").html($(this)[0].userNick);
            $("#paylist .effect-item:nth-child("+n+") h3 a").attr("href","http://www.pulu.com/weixin/otherpeoplepage.html?otherid="+$(this)[0].userId);
            $("#paylist .effect-item:nth-child("+n+") .allday").html($(this)[0].daysOut/100);
            $("#paylist .effect-item:nth-child("+n+") .oneday").html($(this)[0].dayOut/100);
        })
    }
});
$("#paylist").delegate("img","click",function(e){
    var num=$(e.target).data("num");
    location.href="http://www.pulu.com/weixin/otherpeoplepage.html?otherid="+payimgid[num];
});
//切换
$("#incomebtn").click(function(){
    $("#incomelist").css("display","block");
    $("#incomebtn").attr("class","actived");

    $("#paylist").css("display","none");
    $("#paylistbtn").attr("class","");
});
$("#paylistbtn").click(function(){
    $("#incomelist").css("display","none");
    $("#incomebtn").attr("class","");

    $("#paylist").css("display","block");
    $("#paylistbtn").attr("class","actived");
});
//个人账户
$.ajax({
    type:"get",
    url:"https://www.pulu.com/webapi/api/PuluUser/GetUserScores?userId="+id,
    datatype:"json",
    success:function(data){
        $.cookie("cumulative",data.Data.totalIncome+","+data.Data.totalRecharge+","+data.Data.totalOutcome+","+data.Data.totalDesposit);
        $("#userscro").css("display","block");
        $("#userin").html(parseFloat(data.Data.totalIncome)/100);
        $("#userout").html(parseFloat(data.Data.totalOutcome)/100);
        $("#allusernum").html(data.Data.totaluserCount);
        $("#yestinnum").html(data.Data.lastdayAddCount);
    }
});
$("#userscro").click(function(){
    if($.cookie("MYID")){location.href="http://www.pulu.com/weixin/scoredetail.html"}
});
//经验个数
$.ajax({
    type:"get",
    url:"https://www.pulu.com/webapi/api/puluquestion/GetExpCountByUserId?userId="+id,
    datatype:"json",
    success:function(data){
        //console.log(data);
        if(data.Data==0||data.Data==1||data.Data==2){
            $("#cangetimg").css("display","block");
            $("#overimg").css("display","none");
            $("#newpeople i").css("display","block");
            //$("#lastmoney").css("display","block");
            if(data.Data==0){
                $("#lastmoney span:first-child").html(0);
                $("#lastmoney span:last-child").html(3);
            }
            if(data.Data==1){
                $("#lastmoney span:first-child").html(1);
                $("#lastmoney span:last-child").html(2);
            }
            if(data.Data==2){
                $("#lastmoney span:first-child").html(2);
                $("#lastmoney span:last-child").html(1);
            }
        }
        else{
            $("#cangetimg").css("display","none");
            $("#overimg").css("display","block");
        }
        //switch (data.Data){
        //    case 0:
        //        $("#cangetimg").css("display","block");
        //        $("#overimg").css("display","none");
        //        $("#usednum").html("0");
        //        $("#lastnum").html("3");
        //        break;
        //    case 1:
        //        $("#cangetimg").css("display","block");
        //        $("#overimg").css("display","none");
        //        $("#usednum").html("10");
        //        $("#lastnum").html("2");
        //        break;
        //    case 2:
        //        $("#cangetimg").css("display","block");
        //        $("#overimg").css("display","none");
        //        $("#usednum").html("20");
        //        $("#lastnum").html("1");
        //        break;
        //    default :
        //        $("#cangetimg").css("display","none");
        //        $("#overimg").css("display","block");
        //        $("#usednum").html("30");
        //        $("#lastnum").html("0");
        //        break;
        //}
    }
});
//红包
$("#newpeople").click(function(){
    if($.cookie("MYID")){
        $("#detailrule").css("display","block");
    }
});

$("#overimg").click(function(){
    $("#detailrule").css("display","none");
    if(is_weixn()){
        $.cookie("expkeyword","");
        $.cookie("expType","");
        $.cookie("sendque","");
        location.href="http://www.pulu.com/weixin/sendexpgpc.html";
    }else{
        location.href="https://www.pulu.com/weixin/sendexpgpc.html?id="+$.cookie("MYID");
    }
});
$("#cangetimg").click(function(){
    $("#detailrule").css("display","none");
    if(is_weixn()){
        $.cookie("expkeyword","");
        $.cookie("expType","");
        $.cookie("sendque","");
        location.href="http://www.pulu.com/weixin/sendexpgpc.html";
    }else{
        location.href="https://www.pulu.com/weixin/sendexpgpc.html?id="+$.cookie("MYID");
    }
});
//页面跳转
